#include "array.h"
#include "arrayrep.h"

#include <stdio.h>
#include <assert.h>

static void
test()
{
    Array_T array = Array_new(0, 1);
    assert(Array_length(array) == 0);
    assert(Array_size(array) == 1);
    assert(array->array == NULL);
    Array_free(&array);

    array = Array_new(10, 1);
    assert(Array_length(array) == 10);
    assert(Array_size(array) == 1);

    char c;
    c = 'a', Array_put(array, 0, &c);
    c = 'b', Array_put(array, 9, &c);
    assert(*(char*)Array_get(array, 0) == 'a');
    assert(*(char*)Array_get(array, 9) == 'b');

    Array_free(&array);
}

static void
test_get_put()
{
    Array_T array = Array_new(10, sizeof(int));
    int i;
    i = 100, Array_put(array, 0, &i);
    i = 101, Array_put(array, 9, &i);
    assert(*(int*)Array_get(array, 0) == 100);
    assert(*(int*)Array_get(array, 9) == 101);

    Array_T array2 = Array_copy(array, 1);
    assert(Array_length(array2) == 1);
    assert(*(int*)Array_get(array2, 0) == 100);
    
    i = 200, Array_put(array2, 0, &i);
    assert(*(int*)Array_get(array, 0) == 100);
    assert(*(int*)Array_get(array2, 0) == 200);

    Array_T array3 = Array_copy(array, 20);
    assert(*(int*)Array_get(array3, 0) == 100);
    assert(*(int*)Array_get(array3, 9) == 101);
    i = 300, Array_put(array3, 19, &i);
    assert(*(int*)Array_get(array3, 19) == 300);

    Array_free(&array);
    Array_free(&array2);
    Array_free(&array3);
}

static void
test_resize()
{
    Array_T array = Array_new(0, sizeof(int));
    Array_resize(array, 0);
    Array_resize(array, 5);
    Array_resize(array, 10);

    int i;
    i = 100, Array_put(array, 9, &i);
    assert(*(int*)Array_get(array, 9) == 100);
    Array_free(&array);
}

int main()
{
    test();
    test_get_put();
    test_resize();
    return 0;
}
